package com.Darkra1Zzz.controller;


import com.Darkra1Zzz.entity.Admin;
import com.Darkra1Zzz.entity.User;
import com.Darkra1Zzz.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Author DarkraiZzz
 * @Time 2021/6/14 23:08
 * @Version 1.0
 */

@Controller
public class AdminController {
    @Resource
    private AdminService adminService;

    @RequestMapping("tologin")
    public String login(){
        return "login";
    }

    /**
     * 管理员登录
     */
    @RequestMapping("login")
    public String login(String email, String pwd, Model model,HttpSession session){
        System.out.println(email+"-----"+"pwd");
        Admin admin=adminService.login(email,pwd);
        System.out.println(admin+"-------------");
        if (admin!=null){
            model.addAttribute("msg","登陆成功欢迎您:"+admin.getName());
            session.setAttribute("admin", admin);
            return "homepage";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("toRegister")
    public String register(){
        return "register";
    }

    /**
     * 管理员注册
     */
    @RequestMapping("addAdmin")
    public String addAdmin(Admin admin, Model model){
        if (adminService.addAdmin(admin)){
            model.addAttribute("msg","添加成功");
        }else {
            model.addAttribute("msg","添加失败");
        }
        return "register";
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "logout")
    public String loginOut(Admin admin, Model model, HttpSession session) {
        session.invalidate();
        return "login";
    }
}
